package com.ruben.simplegraphql.controller;

import com.ruben.simplegraphql.pojo.dto.GraphqlDTO;
import graphql.ExecutionInput;
import graphql.ExecutionResult;
import graphql.GraphQL;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * UserController
 *
 * @author VampireAchao
 * @since 2022/9/25
 */
@RestController
@RequestMapping("graphql")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class GraphqlController {

    private final GraphQL graphql;

    @PostMapping
    public ExecutionResult graphql(@RequestBody GraphqlDTO dto) {
        return graphql.execute(ExecutionInput.newExecutionInput()
                .query(dto.getQuery())
                .variables(dto.init().getVariables())
                .operationName(dto.getOperationName())
                .build());
    }

}
